from PySide6.QtWidgets import QMainWindow, QVBoxLayout, QHBoxLayout,QGridLayout, QComboBox, QSpinBox, QPushButton, QWidget, QSizePolicy,QCheckBox, QLabel
from PySide6.QtCore import Qt
from app.ui.mainwindow_ui import Ui_MainWindow
from app.main_window_connect import MainWindowConnect

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.fixed_ui = Ui_MainWindow()
        self.fixed_ui.setupUi(self)
        
        self._ui_init()
        self.connect=MainWindowConnect(ui=self)

# region ui 初始化
    def _ui_init(self):
        self.window_init()
        self.fixed_ui.scale_match_bar.setValue(0)  # 设置进度条初始值为0

        # 初始化所有滚动区域
        self.scroll_init(self.fixed_ui.scrollArea_pet)
        self.scroll_init(self.fixed_ui.scrollArea_role)
        self.scroll_init(self.fixed_ui.scrollArea_week)
        self.scroll_init(self.fixed_ui.scrollArea_season)

        self.wlt_init(self.fixed_ui.scrollArea_pet)
        self.zld_init(self.fixed_ui.scrollArea_pet)
        self.douchong_challenge_init(self.fixed_ui.scrollArea_pet)
        self.Mysterious_Merchant_init(self.fixed_ui.scrollArea_role)
        self.baoku_init(self.fixed_ui.scrollArea_role)
        self.signin_init(self.fixed_ui.scrollArea_role)
        self.donate_init(self.fixed_ui.scrollArea_role)
        self.douchong_init(self.fixed_ui.scrollArea_week)
        self.synthesis_gemstone_init(self.fixed_ui.scrollArea_week)
        self.zhuzhan_init(self.fixed_ui.scrollArea_week)
        self.wanshou_init(self.fixed_ui.scrollArea_season)
        
    def window_init(self):
        self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)  # 窗口置顶
        self.setWindowTitle("造梦无双日常辅助工具")
        self.move(360,0)
        self.setFixedSize(1000, 200)

    def scroll_init(self, scroll_area):
        """
        滚动条初始化
        """
        # 设置 scrollArea 的 widgetResizable 属性为 True
        scroll_area.setWidgetResizable(True)

        # 创建一个水平布局用于管理按钮
        scroll_layout = QHBoxLayout()

        # 创建一个容器 QWidget 并设置其布局
        scroll_widget = QWidget()
        scroll_widget.setLayout(scroll_layout)

        # 将容器设置为 scrollArea 的 widget
        scroll_area.setWidget(scroll_widget)
# endregion

# region 事件
    def closeEvent(self, event):
        """窗口关闭时停止线程"""
        self.connect.release_thread()
        event.accept()
# endregion

# region 万灵塔
    def wlt_init(self, scroll_area):
        """
         万灵塔初始化
        """
        # 创建布局
        layout = QVBoxLayout()

        # 创建下拉菜单
        self.wlt_combo_box = QComboBox()
        self.wlt_combo_box.addItems(["头盔", "护甲", "手套", "战靴"])
        sizePolicy = self.wlt_combo_box.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)  
        self.wlt_combo_box.setSizePolicy(sizePolicy)
        layout.addWidget(self.wlt_combo_box)

        # 创建 SpinBox
        self.wlt_spin_box = QSpinBox()
        self.wlt_spin_box.setRange(1, 100)
        self.wlt_spin_box.setValue(1)
        sizePolicy = self.wlt_spin_box.sizePolicy()  
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)  
        self.wlt_spin_box.setSizePolicy(sizePolicy)  
        layout.addWidget(self.wlt_spin_box)

        # 创建按钮
        self.wlt_pb = QPushButton("万灵塔挑战")
        self.wlt_pb.setFixedWidth(150)
        sizePolicy = self.wlt_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)  
        self.wlt_pb.setSizePolicy(sizePolicy)
        layout.addWidget(self.wlt_pb)

        # 创建一个 QWidget 来容纳布局
        wlt_widget = QWidget()
        wlt_widget.setLayout(layout)

        # 将 wlt_widget 添加到 scrollArea 的水平布局里
        scroll_area.widget().layout().addWidget(wlt_widget)
# endregion

# region 神秘商人
    def Mysterious_Merchant_init(self,scroll_area):
        """
        神秘商人初始化
        """
        # 创建布局
        layout = QVBoxLayout()

        # 创建下拉菜单
        self.Mysterious_Merchant_combo_box = QComboBox()
        self.Mysterious_Merchant_combo_box.addItems(["全部最高买满", "全部最低买满", "买一次"])
        sizePolicy = self.Mysterious_Merchant_combo_box.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)  
        self.Mysterious_Merchant_combo_box.setSizePolicy(sizePolicy)
        layout.addWidget(self.Mysterious_Merchant_combo_box)

        # 创建按钮
        self.Mysterious_Merchant_pb = QPushButton("神秘商人")
        self.Mysterious_Merchant_pb.setFixedWidth(130)
        sizePolicy = self.Mysterious_Merchant_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)  
        self.Mysterious_Merchant_pb.setSizePolicy(sizePolicy)
        layout.addWidget(self.Mysterious_Merchant_pb)

        # 创建一个 QWidget 来容纳布局
        shenmi_shangren_widget = QWidget()
        shenmi_shangren_widget.setLayout(layout)

        # 将 shenmi_shangren_widget 添加到 scrollArea 的水平布局里
        scroll_area.widget().layout().addWidget(shenmi_shangren_widget)
# endregion

# region 葬灵洞
    def zld_init(self,scroll_area):
        """
         葬灵洞初始化
        """
        self.zld_pb = QPushButton("葬灵洞挑战")
        sizePolicy = self.zld_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Minimum)
        self.zld_pb.setSizePolicy(sizePolicy)
        self.zld_pb.setFixedWidth(130)
        scroll_area.widget().layout().addWidget(self.zld_pb)
# endregion

# region 斗宠反击
    def douchong_init(self,scroll_area):
        """
         斗宠反击初始化
        """
        self.douchong_pb = QPushButton("斗宠反击")
        sizePolicy = self.douchong_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Minimum)
        self.douchong_pb.setSizePolicy(sizePolicy)
        self.douchong_pb.setFixedWidth(130)
        scroll_area.widget().layout().addWidget(self.douchong_pb)
# endregion

# region 宝库开启
    def baoku_init(self,scroll_area):
        """
        宝库开启初始化
        """
        self.baoku_pb = QPushButton("宝库开启")
        sizePolicy = self.baoku_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Minimum)
        self.baoku_pb.setSizePolicy(sizePolicy)
        self.baoku_pb.setFixedWidth(130)
        scroll_area.widget().layout().addWidget(self.baoku_pb)
# endregion

# region 万兽争锋
    def wanshou_init(self,scroll_area):
        """
        万兽争锋灵兽天梯功能初始化
        """
        self.wanshou_pb = QPushButton("万兽争锋\n灵兽天梯")
        sizePolicy = self.wanshou_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Minimum)
        self.wanshou_pb.setSizePolicy(sizePolicy)
        self.wanshou_pb.setFixedWidth(130)
        scroll_area.widget().layout().addWidget(self.wanshou_pb)
# endregion

# region 合成宝石
    def synthesis_gemstone_init(self,scroll_area):
        """
        合成宝石初始化
        """
         # 创建垂直布局
        main_layout = QVBoxLayout()

        # 创建网格布局用于单选框和标签
        grid_layout = QGridLayout()
        labels_text = ["攻击", "防御", "生命", "回血", "魔法", "回魔", "命中", "闪避", "暴击", "韧性", "幸运", "守护"]
        self.synthesis_gemstone_check_boxes = []
        for i in range(12):
            row = i // 4
            col = i % 4
            # 创建标签
            label = QLabel(labels_text[i])
            grid_layout.addWidget(label, row, col * 2)
            # 创建单选框
            check_box = QCheckBox()
            check_box.setChecked(True)
            grid_layout.addWidget(check_box, row, col * 2 + 1)
            self.synthesis_gemstone_check_boxes.append(check_box)

        # 将网格布局添加到主垂直布局
        main_layout.addLayout(grid_layout)

        # 创建宝石合成按钮
        self.synthesis_gemstone_pb = QPushButton("宝石合成")
        main_layout.addWidget(self.synthesis_gemstone_pb)

        # 创建一个 QWidget 来容纳主布局
        synthesis_widget = QWidget()
        synthesis_widget.setLayout(main_layout)

        # 将包含布局的 QWidget 添加到 scrollArea 的水平布局里
        scroll_area.widget().layout().addWidget(synthesis_widget)
        
# endregion  
  
# region 每日签到
    def signin_init(self,scroll_area):
        """
        签到初始化
        """
        self.signin_pb = QPushButton("每日签到")
        sizePolicy = self.signin_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Minimum)
        self.signin_pb.setSizePolicy(sizePolicy)
        self.signin_pb.setFixedWidth(130)
        scroll_area.widget().layout().addWidget(self.signin_pb)
# endregion

# region 联盟捐献
    def donate_init(self,scroll_area):
        """
        捐献初始化
        """
        layout = QVBoxLayout()

        # 创建 SpinBox
        self.donate_spin_box = QSpinBox()
        self.donate_spin_box.setRange(1, 100)
        self.donate_spin_box.setValue(1)
        sizePolicy = self.donate_spin_box.sizePolicy()  
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)  
        self.donate_spin_box.setSizePolicy(sizePolicy)  
        layout.addWidget(self.donate_spin_box)

        # 创建按钮
        self.donate_pb = QPushButton("联盟捐献")
        self.donate_pb.setFixedWidth(130)
        sizePolicy = self.donate_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)  
        self.donate_pb.setSizePolicy(sizePolicy)
        layout.addWidget(self.donate_pb)

        # 创建一个 QWidget 来容纳布局
        donate_widget = QWidget()
        donate_widget.setLayout(layout)

        # 将 donate_widget 添加到 scrollArea 的水平布局里
        scroll_area.widget().layout().addWidget(donate_widget)
# endregion

# region 获取助战币
    def zhuzhan_init(self,scroll_area):
        """
        助战币初始化
        """
        # 创建垂直布局
        layout = QVBoxLayout()

        # 创建下拉菜单
        self.zhuzhan_combo_box = QComboBox()
        self.zhuzhan_combo_box.addItems(["昆仑西城", "昆仑东城", "昆仑湖"])
        sizePolicy = self.zhuzhan_combo_box.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)
        self.zhuzhan_combo_box.setSizePolicy(sizePolicy)
        layout.addWidget(self.zhuzhan_combo_box)

        # 创建按钮
        self.zhuzhan_pb = QPushButton("获取助战币")
        self.zhuzhan_pb.setFixedWidth(130)
        sizePolicy = self.zhuzhan_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)
        self.zhuzhan_pb.setSizePolicy(sizePolicy)
        layout.addWidget(self.zhuzhan_pb)

        # 创建容器并添加到滚动区域
        zhuzhan_widget = QWidget()
        zhuzhan_widget.setLayout(layout)
        scroll_area.widget().layout().addWidget(zhuzhan_widget)
# endregion

# region 斗宠挑战
    def douchong_challenge_init(self, scroll_area):
        """
        斗宠挑战初始化
        """
        # 创建垂直布局
        layout = QVBoxLayout()

        # 创建下拉菜单
        self.douchong_challenge_combo = QComboBox()
        self.douchong_challenge_combo.addItems(["第一梯队", "第二梯队","单核阵容","牢阵容","入坑新手"])
        # 添加选项提示
        tooltips = [
            "极限三神兽阵容，只打最右边的",
            "极限三宠物阵容，打自身100%战力的宠物", 
            "单极限宠物阵容，打自身90%战力的宠物",
            "单极限，但是宠物是花、冰冰、雪人，打自身80%战力的宠物",
            "新手阵容，不考虑输赢，只打最左边的"
        ]
        for index, tooltip in enumerate(tooltips):
            self.douchong_challenge_combo.setItemData(index, tooltip, Qt.ToolTipRole)
        
        sizePolicy = self.douchong_challenge_combo.sizePolicy()
        sizePolicy = self.douchong_challenge_combo.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)
        self.douchong_challenge_combo.setSizePolicy(sizePolicy)
        layout.addWidget(self.douchong_challenge_combo)

        # 创建按钮
        self.douchong_challenge_pb = QPushButton("斗宠挑战")
        self.douchong_challenge_pb.setFixedWidth(130)
        sizePolicy = self.douchong_challenge_pb.sizePolicy()
        sizePolicy.setVerticalPolicy(QSizePolicy.Expanding)
        self.douchong_challenge_pb.setSizePolicy(sizePolicy)
        layout.addWidget(self.douchong_challenge_pb)

        # 创建容器并添加到滚动区域
        challenge_widget = QWidget()
        challenge_widget.setLayout(layout)
        scroll_area.widget().layout().addWidget(challenge_widget)
# endregion